package fmppc.registrar;

import fmppc.annotation.EnableMyBatisPlus;
import fmppc.scanner.MyBatisEnhanceBeanScanner;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 类描述: MyBatis增强处理注册器
 *
 * @author : 王雷
 * @date : 2020/3/21 0021 下午 6:34
 */
@Slf4j
public class MyBatisPlusImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {
    private ResourceLoader resourceLoader;

    @Override
    public void registerBeanDefinitions(@NotNull AnnotationMetadata importingClassMetadata, @NotNull BeanDefinitionRegistry beanDefinitionRegistry) {

        //这里获取通过@ControolerScan配置的路径信息
        AnnotationAttributes mapperScanAttrs = AnnotationAttributes
                .fromMap(importingClassMetadata.getAnnotationAttributes(EnableMyBatisPlus.class.getName()));

        final List<String> scanPathList = new ArrayList<>(Collections.singletonList("fmppc.config"));

        if(mapperScanAttrs!=null && mapperScanAttrs.getBoolean("isOpenDruid")){
            scanPathList.add("fmppc.druid");
        }

        //这里默认配置扫描的路径，因为路径已经固定了；
//        final String[] strategyPaths = new String[]{"plus.config"};
        final String[] strategyPaths = scanPathList.toArray(new String[0]);

        //TODO 这个对象在后面需要进行统一的抽象封装
        MyBatisEnhanceBeanScanner scanner = new MyBatisEnhanceBeanScanner(beanDefinitionRegistry);
        if (resourceLoader != null) {
            /*
             * 设置资源加载器，这里的资源加载器是通过对象创建时回调的一堆Aware（即：ResourceLoaderAware）来获取的
             * */
            scanner.setResourceLoader(resourceLoader);
        }
//        scanner.registerFilters();
        //构建扫描器，开始扫描指定的路径
        scanner.doScan(strategyPaths);

        /*final DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
        final BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
        beanDefinitionBuilder.addPropertyValue("adder", "");
        defaultListableBeanFactory.registerBeanDefinition("user", beanDefinitionBuilder.getBeanDefinition());*/
    }

    /**
     * Set the ResourceLoader that this object runs in.
     * <p>This might be a ResourcePatternResolver, which can be checked
     * through {@code instanceof ResourcePatternResolver}. See also the
     * {@code ResourcePatternUtils.getResourcePatternResolver} method.
     * <p>Invoked after population of normal bean properties but before an init callback
     * like InitializingBean's {@code afterPropertiesSet} or a custom init-method.
     * Invoked before ApplicationContextAware's {@code setApplicationContext}.
     *
     * @param resourceLoader the ResourceLoader object to be used by this object
     * @see ResourcePatternResolver
     * @see ResourcePatternUtils#getResourcePatternResolver
     */
    @Override
    public void setResourceLoader(@NotNull ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

}
